﻿#region --- 文件描述 ---
//-----------------------------------------------------------------------------
// GameplayScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region --- 命名空间 ---

using System;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using BNB7.Entry;

#endregion

namespace BNB7
{
    /// <summary>
    /// 游戏状态
    /// </summary>
    enum GameState
    { 
        gsStart,        // 开始
        gsRun,          // 运行中
        gsEnd           // 结束
    };

    class GameplayScreen : GameScreen
    {
        #region --- 成员变量 ---

        ContentManager content;
        SpriteFont gameFont;

        GameState _CurState;

        #endregion

        #region --- 初始化 ---

        /// <summary>
        /// Constructor.
        /// </summary>
        public GameplayScreen()
        {
            TransitionOnTime = TimeSpan.FromSeconds(1.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);
        }


        /// <summary>
        /// Load graphics content for the game.
        /// </summary>
        public override void LoadContent()
        {
            if (content == null)
                content = new ContentManager(ScreenManager.Game.Services, "Content");

            gameFont = content.Load<SpriteFont>("gamefont");


            // 初始化素材
            PopoGame.Instance.ScreenManager = ScreenManager;
            PopoGame.Instance.Init();
            PopoGame.Instance.Start();

            // A real game would probably have more content than this sample, so
            // it would take longer to load. We simulate that by delaying for a
            // while, giving you a chance to admire the beautiful loading screen.
            //Thread.Sleep(1000);

            // once the load has finished, we use ResetElapsedTime to tell the game's
            // timing mechanism that we have just finished a very long frame, and that
            // it should not try to catch up.
            ScreenManager.Game.ResetElapsedTime();
        }


        /// <summary>
        /// Unload graphics content used by the game.
        /// </summary>
        public override void UnloadContent()
        {
            content.Unload();
        }

        #endregion

        #region --- Update and Draw ---

        /// <summary>
        /// Updates the state of the game. This method checks the GameScreen.IsActive
        /// property, so the game will stop updating when the pause menu is active,
        /// or if you tab away to a different application.
        /// </summary>
        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            if (IsActive)
            {
                // 更新游戏数据
                PopoGame.Instance.Update(gameTime);
            }
        }


        /// <summary>
        /// Lets the game respond to player input. Unlike the Update method,
        /// this will only be called when the gameplay screen is active.
        /// </summary>
        public override void HandleInput(InputState input)
        {
            if (input == null)
                throw new ArgumentNullException("input");

            // 控制器
            PopoGame.Instance.ProcessInput(input);

            // Look up inputs for the active player profile.
            //int playerIndex = (int)ControllingPlayer.Value;

            //KeyboardState keyboardState = input.CurrentKeyboardStates[playerIndex];
            //GamePadState gamePadState = input.CurrentGamePadStates[playerIndex];

            //// if the user pressed the back button, we return to the main menu
            //PlayerIndex player;
            //if (input.IsNewButtonPress(Buttons.Back, ControllingPlayer, out player))
            //{
            //    LoadingScreen.Load(ScreenManager, false, ControllingPlayer, new BackgroundScreen(), new MainMenuScreen());
            //}
            //else
            //{
            //    // Otherwise move the player position.
            //    //Vector2 movement = Vector2.Zero;

            //    //if (keyboardState.IsKeyDown(Keys.Left))
            //    //    movement.X--;

            //    //if (keyboardState.IsKeyDown(Keys.Right))
            //    //    movement.X++;

            //    //if (keyboardState.IsKeyDown(Keys.Up))
            //    //    movement.Y--;

            //    //if (keyboardState.IsKeyDown(Keys.Down))
            //    //    movement.Y++;

            //    //Vector2 thumbstick = gamePadState.ThumbSticks.Left;

            //    //movement.X += thumbstick.X;
            //    //movement.Y -= thumbstick.Y;

            //    //if (movement.Length() > 1)
            //    //    movement.Normalize();

            //    //playerPosition += movement * 2;
            //}
        }


        /// <summary>
        /// Draws the gameplay screen.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            ScreenManager.SpriteBatch.Begin();

            PopoGame.Instance.Draw(gameTime);

            ScreenManager.SpriteBatch.End();
        }

        #endregion

    }
}
